package weeek3.experience;
public class LinkedList {
    public int nDuanZhiXuan = 0;

    public node insernode(int data, node head) {
        node previous = head;
        node node1 = new node(data);
        if (head == null) {
            head = node1;
            return head;
        } else {
            while (previous.next != null) {
                previous = previous.next;
            }
            previous.next = node1;
            return head;
        }
    }//这是尾插法的实现方法

    public void printNode(node head) {
        node tmpNode = head;
        while (tmpNode != null) {
            System.out.print(tmpNode.number + " ");
            tmpNode = tmpNode.next;
        }
    }//这是链表的打印方法

    public int length(node head) {
        node current = head;
        nDuanZhiXuan = 1;
        while (current.next != null) {
            current = current.next;
            nDuanZhiXuan++;
        }
        return nDuanZhiXuan;
    }//这是统计链表元素的方法


    public node insernode(int data, int index, node head) {
        node previous = head;
        node current = head.next;
        node node1 = new node(data);
        if (index == 0 && head == null) {
            head = node1;
            return head;
        }
        if (index == 0 && head != null) {
            node1.next = head;
            head = node1;
            return head;
        }
        if (index == length(head)) {
            while (previous.next != null) {
                previous = previous.next;
            }
            previous.next = node1;
            return head;
        } else {
            for (int a = 1; a < index; a++) {
                previous = previous.next;
                current = current.next;
            }
            previous.next = node1;
            node1.next = current;

            return head;
        }

    }//这是实现按索引插入的方法。

    public node deletenode(int index, node head) {

        if (index == 0) {
            head = head.next;

            return head;
        }
        if (index == length(head) && length(head) != 0) {
            node current = head.next;
            node previous = head;

            while (current.next != null) {
                previous = previous.next;
                current = current.next;
            }
            previous.next = null;
            return head;
        } else {
            node current = head.next;
            node previous = head;
            for (int a = 1; a < index; a++) {
                current = current.next;
                previous = previous.next;
            }
            previous.next = current.next;
            return head;
        }
    }//这是按索引删除的方法

    public node deletenode(node head, int number) {
        node current = head;
        node current1 = head;
        node previous = head.next;
        if (current.number == number) {
            head = head.next;
            return head;
        }
        while (current1.next != null && current1.next.next != null) {
            current1 = current1.next;
            previous = previous.next;
            if (current1.next.number == number && previous.next != null)
                current1.next = previous.next;
            if (current1.next == previous.next)
                break;
        }


        while (current.next.next != null) {
            current = current.next;

        }
        if (current.next.number == number)
            current.next = null;
        return head;
    }//这是实现按元素删除的方法

    public node selectsort(node head) {
        node current = head;
        int num ;
        while (current.next != null) {
            node list = current.next;
            while (list != null) {
                if (list.number < current.number) {
                    num = list.number;

                    list.number = current.number;
                    current.number = num;
                }
                list = list.next;

            }
            current=current.next;
            length(head);
            System.out.print("元素总数为"+nDuanZhiXuan+" ");
            printNode(head);
            System.out.println("\n");
        }
    return head;
    }
}